/*
** show_alloc_mem.c for  in /home/alxevo/Documents/projets/malloc/malloc_norme
** 
** Made by Thibault Ravera
** Login   <ravera_t@epitech.net>
** 
** Started on  Sat Feb  4 23:06:37 2012 Thibault Ravera
** Last update Sun Feb  5 23:09:09 2012 Thibault Ravera
*/

#include	"malloc.h"

void            my_putchar(char c)
{
  write(1, &c, 1);
}

void		my_putstr(char *str)
{
  write(1, str, strlen(str));
}

void            my_putnbr(size_t nb, char *base, int length)
{
  if (nb >= (size_t)length)
    my_putnbr(nb / length, base, length);
  else if (length == 16)
    my_putstr("0x");
  my_putchar(base[(nb % length)]);
}

void		print_this_chunk(void *ptr)
{
  my_putnbr((size_t)ptr, "0123456789ABCDEF", 16);
  my_putstr(" - ");
  my_putnbr((size_t)(ptr + ((MEM2CHUNK(ptr)->size & ~PINUSE) - _MYSIZE_T)),
	    "0123456789ABCDEF", 16);
  my_putstr(" : ");
  my_putnbr(MEM2CHUNK(ptr)->size & ~PINUSE, "0123456789", 10);
  my_putstr(" octets\n");
}

void		show_alloc_mem()
{
  t_data_range	*tmp;
  void		*head;

  tmp = DATA_RANGE(head_heap_ori);
  while (tmp)
    {
      head = ((void*)tmp + 32);
      my_putstr("break: ");
      my_putnbr((size_t)tmp->queue, "0123456789abcdef", 16);
      my_putchar('\n');
      while ((size_t)head < (size_t)(tmp->queue - _MYSIZE_T))
	{
	  print_this_chunk(head);
	  head = (head + (MEM2CHUNK(head)->size & ~PINUSE));
	}
      tmp = tmp->next;
    }
  my_putchar('\n');
}
